package com.ybxx.system.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.ybxx.core.model.BaseModel;




@Entity
@Table(name="tbl_system_permission")
public class Permission extends BaseModel{
	@Id
	private String pid;
	@ManyToOne(targetEntity=Permission.class)  
	@JoinColumn(name="fid",referencedColumnName="pid") 
	private Permission parent;
	private String pname;
	private String purl;
	private int seq;
	private String style;
	@ManyToMany(targetEntity=User.class,mappedBy="permissions",fetch = FetchType.LAZY)
	private Set<User> users=new HashSet<User>();
	@OneToMany(targetEntity=Permission.class,cascade={CascadeType.ALL},mappedBy="parent",fetch = FetchType.LAZY)
	private Set<Permission> childs=new HashSet<Permission>();
	@Transient
	private String fid;
	@Transient
	private boolean checked;
	@Transient
	private List<Permission> sortChilds=new ArrayList<Permission>();
	
	
	/**
	 * 
	 */
	public Permission() {
	}
	
	/**
	 * @param pid
	 * @param parent
	 * @param pname
	 * @param purl
	 * @param seq
	 * @param style
	 */
	public Permission(String pid, String pname, String purl,
			int seq, String style) {
		this.pid = pid;
		this.pname = pname;
		this.purl = purl;
		this.seq = seq;
		this.style = style;
	}

	public String getPid() {
		return pid;
	}
	public void setPid(String pid) {
		this.pid = pid;
	}
	public Permission getParent() {
		return parent;
	}
	public void setParent(Permission parent) {
		this.parent = parent;
	}
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}
	public String getPurl() {
		return purl;
	}
	public void setPurl(String purl) {
		this.purl = purl;
	}
	public int getSeq() {
		return seq;
	}
	public void setSeq(int seq) {
		this.seq = seq;
	}
	public String getStyle() {
		return style;
	}
	public void setStyle(String style) {
		this.style = style;
	}
	public Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	public Set<Permission> getChilds() {
		return childs;
	}
	public void setChilds(Set<Permission> childs) {
		this.childs = childs;
		
	}

	public String getFid() {
		return fid;
	}

	public void setFid(String fid) {
		this.fid = fid;
	}

	public boolean getChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
	}

	public List<Permission> getSortChilds() {
		if(this.sortChilds.size()<=0){
			setSortChilds();
		}
		return sortChilds;
	}

	public void setSortChilds() {
		this.sortChilds.addAll(childs);
		Collections.sort(this.sortChilds, new Comparator<Permission>(){
			public int compare(Permission o1, Permission o2) {
				if(o1.getSeq()<o2.getSeq()){
					return -1;
				}else if(o1.seq==o2.seq){
					return 0;
				}else{
					return 1;
				}
			}});
	}
	
	
	
	
}
